home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-13 | 3.9 KB | 212 lines | [TEXT/EDIT] |
- -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C)
- -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
- --
- class FIXED_ARRAY[E]
- --
- -- Unlike ARRAY, the `lower' bound of a FIXED_ARRAY is
- -- frozen (to 0). Thus, when looping toward 0, code may
- -- run (just) a little bit faster than ARRAY.
- --
- -- Note: a FIXED_ARRAY as only two attributes it save
- -- a little bit memory too :-)
- --
-
- inherit
- COLLECTION[E]
- redefine is_equal, fast_nb_occurrences, all_cleared
- end;
-
- creation {ANY}
- make, resize, from_collection
-
- feature
-
- lower: INTEGER is 0;
- -- Lower index bound.
-
- upper: INTEGER;
- -- Upper index bound.
-
- feature {NONE}
-
- storage: POINTER;
- -- Internal access to storage location.
- -- Corresoponding C type is computed according
- -- to generic type E.
-
- feature -- Creation and Modification :
-
- make(size: INTEGER) is
- require
- size >= 0
- do
- if storage.is_not_void then
- storage := realloc(storage,size);
- else
- storage := malloc(size);
- end;
- upper := size - 1;
- clear_all;
- ensure
- count = size;
- all_cleared
- end;
-
- from_collection(cltn: COLLECTION[E]) is
- local
- fa_index, cltn_index: INTEGER;
- do
- from
- make(cltn.count);
- fa_index := upper;
- cltn_index := cltn.upper;
- until
- fa_index < 0
- loop
- put(cltn.item(cltn_index),fa_index);
- cltn_index := cltn_index - 1;
- fa_index := fa_index - 1;
- end;
- ensure
- count = cltn.count
- end;
-
- feature -- Accessing :
-
- infix "@", item(index: INTEGER): E is
- external "CSE"
- end;
-
- feature -- Modification :
-
- put(element: E; index: INTEGER) is
- external "CSE"
- end;
-
- clear is
- -- Empty the array, discard all items.
- do
- upper := -1;
- end;
-
- copy(other: like Current) is
- -- Copy `other' onto Current.
- local
- i: INTEGER;
- do
- if upper /= other.upper then
- make(other.upper);
- end;
- from
- i := upper;
- until
- i = lower
- loop
- put(other.item(i),i);
- i := i - 1;
- end;
- end;
-
- feature -- Looking and comparison :
-
- is_equal(other: like Current): BOOLEAN is
- -- Use `equal' to compare elements.
- local
- i: INTEGER;
- e1, e2: E;
- do
- if Current = other then
- Result := true;
- elseif upper = other.upper then
- from
- Result := true;
- i := upper;
- until
- i < 0 or not Result
- loop
- Result := equal_like(item(i),other.item(i));
- i := i - 1;
- end;
- end;
- end;
-
- all_cleared: BOOLEAN is
- -- Are all items set to default values?
- local
- value: E;
- i: INTEGER;
- do
- from
- Result := true;
- i := upper;
- until
- i < lower
- loop
- Result := value = item(i);
- if Result then
- i := i - 1;
- else
- i := -1;
- end;
- end;
- end;
-
- fast_nb_occurrences(elt: E): INTEGER is
- -- Number of occurrences using `='.
- local
- i: INTEGER;
- do
- from
- i := upper;
- until
- i < 0
- loop
- if elt = item(i) then
- Result := Result + 1;
- end;
- i := i - 1;
- end;
- end;
-
- feature -- Interfacing with C :
-
- to_external: POINTER is
- -- Gives C access into the internal `storage' of the ARRAY.
- -- Result is pointing the element at index `lower'.
- --
- -- NOTE: do not free/realloc the Result. Resizing of the array
- -- can makes this pointer invalid.
- require
- not empty
- do
- Result := storage;
- ensure
- Result.is_not_void
- end;
-
- feature {NONE}
-
- malloc(size: INTEGER): POINTER is
- require
- size > 0
- local
- x: like item;
- do
- c_inline_c("R=malloc((size_t)(a1*sizeof(_x)));");
- end;
-
- realloc(pointer: POINTER; size: INTEGER): POINTER is
- require
- size > 0
- local
- x: like item;
- do
- c_inline_c("R=realloc(a1,(size_t)(a2*sizeof(_x)));");
- end;
-
- invariant
-
- upper >= lower implies storage /= Void;
-
- end -- FIXED_ARRAY[E]
-